What will this program write to the monitor?
New location:java.awt.Point[x=12,y=42]
Automatic Conversions |
---|
|
In general, if information might be LOST, a conversion from one type to another will NOT be performed automatically. A conversion from a data type that uses N bits to a type that uses fewer than N bits risks information loss, and will NOT be performed automatically. The compiler makes this decision by examining the data types involved, not the actual values involved.
Note that it is the data types involved and not the actual data currently in a variable that determines if a conversion is safe.
"Loss of precision" means that some of the less significant digits may become zeros,
but the most important digits and the size of the number will remain.
Recall (from chapter 8) that float
has only about
seven decimal digits of precision.
For example, converting the int
123456789 to
a float
123456700.0 shows a loss of precision.
Is there a loss of precision in converting
the int
123456789 to
a double
in the following?
int a = 123456789; double x = a;